import 'dart:developer';
import 'dart:io';
import 'package:flutter/services.dart';
import 'package:kmxy_base/base/base_package.dart';
import 'package:kmxy_base/page/signature/signature_page_view.dart';
import 'package:kmxy_base/util/amap_location_util.dart';
import 'package:kmxy_base/util/image_util.dart';
import 'package:kmxy_base_example/page/page_manage_package.dart';

abstract class MainPageState extends BasePageState<MainPage> {
  String? imageFile;

  @override
  initData() {
    log((1.9).toInt().toString());

    if (Platform.isAndroid) {
      //延时执行
      Future.delayed(const Duration(milliseconds: 500), () async {
        PermissionUtil.getInstance.permissionDialog(context);
      });
    }
  }

  @override
  destroyData() {}
  @override
  eventListen(Event event) {
    super.eventListen(event);
    // list();
  }

  list() {
    push(context, ListPage());
  }

  network() {
    push(context, NetworkPage());
  }

  oneDialog() {
    DialogFactory.pickerS(
        context,
        [
          'dsada',
          'dasdada',
          'dasdada',
          'dasdada',
          'dasdada',
          'dasdada',
          'dasdada'
        ],
        onConfirm: (data) {},
        onCancel: () {});
  }

  twoDialog() {}

  getPath() async {}

  permission() async {
    PermissionUtil.getInstance.permissionDialog(context);
  }

  patrolPermission() async {
    PermissionUtil.getInstance.patrolPermissionDialog(context);
  }

  dateTime() {
    DialogFactory.dateTimeSelect(context, onTime: (data) {});
  }

  date() {
    DialogFactory.pickerYearMonth(context, onConfirm: () {}, onCancel: () {});
  }

  cameraAndGallery() {
    DialogFactory.cameraAndGallery(context,
        (List filePathList, int type) async {
      log(filePathList.toString());
      String pathM = await ImageUtil.getInstance.imageTextWatermark(
          "时间：2023-12-22 12:22:30\n位置：云南省昆明市五华区新明巷2001号靠近国家环境保护工业资源循环利用工程技术中心\n维度：25.078768\n经度：102.652595",
          filePathList[0]);
      await FileUtil.saveImgOrVideoToGallery(pathM, isDelSourceFile: true);
    });
  }

  map() {}

  startMic() {
    MicUtil.micStart();
  }

  String micPathUrl = '';

  endMic() {
    MicUtil.micStop((micPath, time) {
      micPathUrl = micPath;
      micPath.log();
      time.toString().log();
    });
  }

  startPlay() {
    MicUtil.playSound(micPathUrl, () {
      '准备播放'.log();
    }, () {
      '播放完成'.log();
    });
  }

  endPlay() {
    MicUtil.stopSound(() => {});
  }

  camera() async {
    String? path = await CameraUtil.openCamera(context, 80, 1080, 1920);
    '$path'.toast();
    setState(() {
      imageFile = path;
    });
  }

  pdf() {
    push(context, PdfPage());
  }

  webview() {
    push(context, WebviewPage());
  }

  form() {
    push(context, FormPage());
  }

  amapLocation() {
    // push(context, AmapLocationPage());
  }

  callPhone() {
    KmxyBase.callPhone('123456');
  }

  locationData() {
    push(context, LocationDataPage());
  }

  List selectCheckList = [
    {'name': '技术肯定就萨拉酱', 'id': 1, 'check': false},
    {'name': '三生三世', 'id': 2, 'check': false},
    {'name': '小苏打', 'id': 3, 'check': false},
    {'name': '学习心得', 'id': 4, 'check': false},
    {'name': '问问', 'id': 5, 'check': false},
    {'name': '顶顶顶顶', 'id': 6, 'check': false},
    {'name': '顶顶顶顶', 'id': 6, 'check': false},
    {'name': '顶顶顶顶', 'id': 6, 'check': false},
    {'name': '顶顶顶顶', 'id': 6, 'check': false},
    {'name': '顶顶顶顶', 'id': 6, 'check': false},
    {'name': '顶顶顶顶', 'id': 6, 'check': false},
    {'name': '顶顶顶顶', 'id': 6, 'check': false},
    {'name': '顶顶顶顶', 'id': 6, 'check': false},
    {'name': '顶顶顶顶', 'id': 6, 'check': false},
    {'name': '顶顶顶顶', 'id': 6, 'check': false},
    {'name': '顶顶顶顶', 'id': 6, 'check': false},
    {'name': '顶顶顶顶', 'id': 6, 'check': false},
    {'name': '顶顶顶顶', 'id': 6, 'check': false},
    {'name': '顶顶顶顶', 'id': 6, 'check': false},
    {'name': '顶顶顶顶', 'id': 6, 'check': false},
    {'name': '顶顶顶顶', 'id': 6, 'check': false},
    {'name': '顶顶顶顶', 'id': 6, 'check': false},
    {'name': '顶顶顶顶', 'id': 6, 'check': false},
    {'name': '顶顶顶顶', 'id': 6, 'check': false},
    {'name': '顶顶顶顶', 'id': 6, 'check': false},
    {'name': '顶顶顶顶', 'id': 6, 'check': false},
    {'name': '顶顶顶顶', 'id': 6, 'check': false},
    {'name': '顶顶顶顶', 'id': 6, 'check': false},
    {'name': '顶顶顶顶', 'id': 6, 'check': false},
    {'name': '顶顶顶顶', 'id': 6, 'check': false},
    {'name': '顶顶顶顶', 'id': 6, 'check': false},
    {'name': '顶顶顶顶', 'id': 6, 'check': false},
    {'name': '顶顶顶顶', 'id': 6, 'check': false},
    {'name': '顶顶顶顶', 'id': 6, 'check': false},
    {'name': '顶顶顶顶', 'id': 6, 'check': false},
    {'name': '顶顶顶顶', 'id': 6, 'check': false},
    {'name': '顶顶顶顶', 'id': 6, 'check': false},
    {'name': '顶顶顶顶', 'id': 6, 'check': false},
    {'name': '顶顶顶顶', 'id': 6, 'check': false},
    {'name': '顶顶顶顶', 'id': 6, 'check': false},
    {'name': '顶顶顶顶', 'id': 6, 'check': false},
    {'name': '顶顶顶顶', 'id': 6, 'check': false},
    {'name': '顶顶顶顶', 'id': 6, 'check': false},
  ];

  selectCheck() async {
    Map? callback = await push(
      context,
      SelectCheckPage(
        arguments: {
          'style': {
            'centerTitle': true,
            'backgroundColor': const Color(0xff4563AB),
            'title': '选择巡查人',
            'color': Colors.white,
            'fontSize': 16.sp,
            'fontWeight': FontWeight.w700,
          },
          'dataList': selectCheckList,
          'name': 'name',
        },
      ),
    );
    if (callback != null) {
      List callbackList = callback['checkList'];
      log(callbackList.toString());
      for (Map dataY in selectCheckList) {
        dataY['check'] = false;
        for (Map dataN in callbackList) {
          if (dataY['id'] == dataN['id']) {
            dataY['check'] = true;
            break;
          }
        }
      }
    }
  }

  requestIgnoreBatteryOptimizationsCheck() {
    KmxyBase.requestIgnoreBatteryOptimizations();
  }

  aMapLatLngListTest() async {
    // List tempxxx = [];
    // for (int i = 0; i < AMapLocationUtil.getInstance.osXxxx.length; i++) {
    //   tempxxx.add(AMapLocationUtil.getInstance.osXxxx[i]);
    //   if (tempxxx.length == 100) {
    //     break;
    //   }
    // }
    // AMapLocationUtil.getInstance.osXxxx.clear();
    // AMapLocationUtil.getInstance.osXxxx.addAll(tempxxx);
    // List aMapLatLngList = await AMapLocationUtil.getInstance.upAmapLatLngList(
    //     AMapLocationUtil.getInstance.osXxxx, "denoise=1,mapmatch=1");
    // log(aMapLatLngList.toString());
    // log(aMapLatLngList.length.toString());
  }

  signature() async {
    Map? callData = await push(
        context,
        SignaturePage(
          arguments: const {
            'style': {
              'backgroundColor': Colors.blue,
              'color': Colors.white,
            }
          },
        ));
    if (callData != null) {
      log(callData['path']);
    }
  }

  gps() async {
    bool isOpenGps = await AMapLocationUtil.getInstance.isOpenGps();
    log(isOpenGps.toString());
  }

  getpath() async {
    String path = await FileUtil.getFilesDir();
    log(path);
  }

  setting() async {
    push(context, SettingPage());
  }

  List tempList = [
    {
      "lrnm": "河流",
      "lrtp": "0",
      "children": [
        {
          "lrtp": "0",
          "lrcd": "001",
          "lrnm": "测试但是捡垃圾大垃圾袋老司机阿拉",
          "children": [
            {"lrtp": "1", "lrcd": "SK001", "lrnm": "测试水库222", "children": null},
            {"lrtp": "2", "lrcd": "SK002", "lrnm": "测试水库", "children": null}
          ]
        },
        {
          "lrtp": "0",
          "lrcd": "001",
          "lrnm": "测试",
          "children": [
            {"lrtp": "1", "lrcd": "SK001", "lrnm": "测试水库222", "children": null},
            {"lrtp": "2", "lrcd": "SK002", "lrnm": "测试水库", "children": null}
          ]
        }
      ]
    },
    {
      "lrnm": "水库",
      "lrtp": "1",
      "children": [
        {
          "lrtp": "1",
          "lrcd": "SK001",
          "lrnm": "测试水库",
          "children": [
            {"lrtp": "1", "lrcd": "SK001", "lrnm": "测试水库", "children": null},
            {"lrtp": "2", "lrcd": "SK002", "lrnm": "测试水库3121", "children": null}
          ]
        },
        {"lrtp": "1", "lrcd": "sk-003", "lrnm": "赵子河水库", "children": null},
        {"lrtp": "1", "lrcd": "sk-001", "lrnm": "升钟水库", "children": null},
        {"lrtp": "1", "lrcd": "sk-002", "lrnm": "王大湾水库", "children": null},
        {"lrtp": "1", "lrcd": "sk-004", "lrnm": "龙台山水库", "children": null},
        {"lrtp": "1", "lrcd": "sk-005", "lrnm": "应家沟水库", "children": null},
        {"lrtp": "1", "lrcd": "sk-006", "lrnm": "莲花湖", "children": null},
        {"lrtp": "1", "lrcd": "sk-007", "lrnm": "九龙潭", "children": null},
        {"lrtp": "1", "lrcd": "sk-008", "lrnm": "青龙湖", "children": null},
        {"lrtp": "1", "lrcd": "111", "lrnm": "测试水库-浸润线", "children": null},
        {"lrtp": "1", "lrcd": "sk-009", "lrnm": "八尔湖", "children": null},
        {"lrtp": "1", "lrcd": "SK1112", "lrnm": "大屯海水库", "children": null},
        {"lrtp": "1", "lrcd": "SK002", "lrnm": "测试水库2", "children": null}
      ]
    },
    {
      "lrnm": "渠道",
      "lrtp": "2",
      "children": [
        {"lrtp": "2", "lrcd": "QD_I9PQ", "lrnm": "白七斗渠", "children": null},
        {"lrtp": "2", "lrcd": "QD_I26E", "lrnm": "南溪分支渠", "children": null},
        {"lrtp": "2", "lrcd": "QD_I9DV", "lrnm": "白塔斗渠", "children": null},
        {"lrtp": "2", "lrcd": "QD_IXBZ", "lrnm": "永清斗渠", "children": null},
        {"lrtp": "2", "lrcd": "QD_IXVR", "lrnm": "中心分支渠", "children": null},
        {"lrtp": "2", "lrcd": "QD_IXJG", "lrnm": "彭城分支渠", "children": null},
        {"lrtp": "2", "lrcd": "QD_IX7B", "lrnm": "柏垭分支渠", "children": null},
        {"lrtp": "2", "lrcd": "QD_IXSU", "lrnm": "兴隆分支渠", "children": null},
        {"lrtp": "2", "lrcd": "QD_IXWP", "lrnm": "观音分支渠", "children": null},
        {"lrtp": "2", "lrcd": "QD_IZYK", "lrnm": "窑场分支渠", "children": null},
        {"lrtp": "2", "lrcd": "QD_IZ3E", "lrnm": "碧龙分支渠", "children": null},
        {"lrtp": "2", "lrcd": "QD_IZ7I", "lrnm": "龙大山分支渠", "children": null},
        {"lrtp": "2", "lrcd": "QD_IZD3", "lrnm": "梅子垭分支渠", "children": null},
        {"lrtp": "2", "lrcd": "QD_IZQC", "lrnm": "梨树垭提灌分支渠", "children": null},
        {"lrtp": "2", "lrcd": "QD_IDU5", "lrnm": "马鞍分支渠", "children": null},
        {"lrtp": "2", "lrcd": "QD_ID5R", "lrnm": "共兴分支渠", "children": null},
        {"lrtp": "2", "lrcd": "QD_ID9D", "lrnm": "大村沟分支渠", "children": null},
        {"lrtp": "2", "lrcd": "QD_ID26", "lrnm": "东太分支渠", "children": null},
        {"lrtp": "2", "lrcd": "QD_IDWS", "lrnm": "李家分支渠", "children": null},
        {"lrtp": "2", "lrcd": "QD_I5KS", "lrnm": "金源斗渠", "children": null},
        {"lrtp": "2", "lrcd": "QD_I742", "lrnm": "扶黄斗渠", "children": null},
        {"lrtp": "2", "lrcd": "QD_I5Z7", "lrnm": "大河斗渠", "children": null},
        {"lrtp": "2", "lrcd": "QD_I58B", "lrnm": "蓬镇斗渠", "children": null},
        {"lrtp": "2", "lrcd": "QD_IPB4", "lrnm": "金台斗渠", "children": null},
        {"lrtp": "2", "lrcd": "QD_IPUJ", "lrnm": "青龙湖提灌渠", "children": null},
        {"lrtp": "2", "lrcd": "QD_IP92", "lrnm": "八尔湖充水渠", "children": null},
        {"lrtp": "2", "lrcd": "QD_IP23", "lrnm": "荆溪斗渠", "children": null},
        {"lrtp": "2", "lrcd": "QD_I7GV", "lrnm": "马市铺斗渠", "children": null},
        {"lrtp": "2", "lrcd": "QD_I7T8", "lrnm": "永丰斗渠", "children": null},
        {"lrtp": "2", "lrcd": "QD_I94P", "lrnm": "光华斗渠", "children": null},
        {"lrtp": "2", "lrcd": "QD_I7CB", "lrnm": "车龙斗渠", "children": null},
        {"lrtp": "2", "lrcd": "QD_I7A7", "lrnm": "占山斗渠", "children": null},
        {"lrtp": "2", "lrcd": "QD_ICB5", "lrnm": "观凤斗渠", "children": null},
        {"lrtp": "2", "lrcd": "QD_ICY2", "lrnm": "中凤斗渠", "children": null},
        {"lrtp": "2", "lrcd": "QD_ICUZ", "lrnm": "新华斗渠", "children": null},
        {"lrtp": "2", "lrcd": "QD_IC9S", "lrnm": "石龙斗渠", "children": null},
        {"lrtp": "2", "lrcd": "QD_IC2B", "lrnm": "万年斗渠", "children": null},
        {"lrtp": "2", "lrcd": "QD_IC8A", "lrnm": "老观斗渠", "children": null},
        {"lrtp": "2", "lrcd": "QD_I96Y", "lrnm": "度门斗渠", "children": null},
        {"lrtp": "2", "lrcd": "QD_I8EM", "lrnm": "左分干渠", "children": null},
        {"lrtp": "2", "lrcd": "QD_IETK", "lrnm": "西蓬分干渠", "children": null},
        {"lrtp": "2", "lrcd": "QD_IEJ5", "lrnm": "西南分干渠", "children": null},
        {"lrtp": "2", "lrcd": "QD_IEI8", "lrnm": "盐溪分干渠", "children": null},
        {"lrtp": "2", "lrcd": "QD_IEX6", "lrnm": "南充干渠", "children": null},
        {"lrtp": "2", "lrcd": "QD_IEAP", "lrnm": "西充干渠", "children": null},
        {"lrtp": "2", "lrcd": "QD_542U", "lrnm": "右总干渠", "children": null},
        {"lrtp": "2", "lrcd": "QD_I82W", "lrnm": "五龙支渠", "children": null},
        {"lrtp": "2", "lrcd": "QD_ISQ3", "lrnm": "华光支渠", "children": null},
        {"lrtp": "2", "lrcd": "QD_I25R", "lrnm": "群乐分支渠", "children": null},
        {"lrtp": "2", "lrcd": "QD_I2ZG", "lrnm": "蒙升分支渠", "children": null},
        {"lrtp": "2", "lrcd": "QD_I28G", "lrnm": "群德分支渠", "children": null},
        {"lrtp": "2", "lrcd": "QD_ISGJ", "lrnm": "龙凤分支渠", "children": null},
        {"lrtp": "2", "lrcd": "QD_ISYK", "lrnm": "大河坝分支渠", "children": null},
        {"lrtp": "2", "lrcd": "QD_IS3Z", "lrnm": "复兴分支渠", "children": null},
        {"lrtp": "2", "lrcd": "QD_IS59", "lrnm": "三元分支渠", "children": null},
        {"lrtp": "2", "lrcd": "QD_ISXV", "lrnm": "大观分支渠", "children": null},
        {"lrtp": "2", "lrcd": "QD_IS2I", "lrnm": "木老分支渠", "children": null},
        {"lrtp": "2", "lrcd": "QD_I2U7", "lrnm": "高农分支渠", "children": null},
        {"lrtp": "2", "lrcd": "QD_IAYE", "lrnm": "石河支渠", "children": null},
        {"lrtp": "2", "lrcd": "QD_IA32", "lrnm": "大桥支渠", "children": null},
        {"lrtp": "2", "lrcd": "QD_IACV", "lrnm": "定水支渠", "children": null},
        {"lrtp": "2", "lrcd": "QD_IA2F", "lrnm": "流马支渠", "children": null},
        {"lrtp": "2", "lrcd": "QD_IAQU", "lrnm": "盘龙支渠", "children": null},
        {"lrtp": "2", "lrcd": "QD_I8RT", "lrnm": "石楼分支渠", "children": null},
        {"lrtp": "2", "lrcd": "QD_I8KA", "lrnm": "武胜支渠", "children": null},
        {"lrtp": "2", "lrcd": "QD_I8C4", "lrnm": "李渡支渠", "children": null}
      ]
    }
  ];

  mapPickerMore() {
    DialogFactory.pickerMapMore(context, tempList, 'lrnm', titleText: '算我输标题',
        onConfirm: (List selectList, Map selectData) {
      log(selectList.toString());
      log(selectData.toString());
    }, onCancel: () {});
  }
}
